//
//  BadminMoneyViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/5/22.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BadminMoneyViewController.h"
#import "BadminMoneyListTableViewCell.h"
#import "BadminMoneyModel.h"
#import "BadminMoneyHeader.h"
#import "CSSettlementViewController.h"
#import "BselegetMoneyJLViewController.h"
#import "BgetMoneyJLViewController.h"
#import "BillDetailViewController.h"

@interface BadminMoneyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@property(nonatomic,assign)NSInteger dayind;

@property(nonatomic,strong)BadminMoneyHeader *headerView;


@end
@implementation BadminMoneyViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"资金管理";
    // Do any additional setup after loading the view.
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
//    [self.myTableView reloadData];
    self.headerView = [[BadminMoneyHeader alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 270)];
    kWeakSelf;
    self.headerView.mybloc = ^(NSString * _Nonnull str) {
        NSInteger ind = [str integerValue];
        switch (ind) {
            case 0:
                {
                    [NetWorkTool POST:BMonygetSetMoney param:nil success:^(id dic) {
                        [weakSelf.navigationController pushViewController:[CSSettlementViewController new] animated:YES];

                    } other:^(id dic) {
                        ShowToastWithText(dic[@"msg"]);
                    } fail:^(NSError *error) {
                        
                    } needUser:YES];
                    
                }
                break;
            case 1:
            {//今日营业额
                BselegetMoneyJLViewController *upvc = [[BselegetMoneyJLViewController alloc]init];
                upvc.moneyStr = weakSelf.headerView.jinriYongyee.text;
                upvc.typeSYstr = @"1";
                upvc.titleStr = @"收益明细";
                [self.navigationController pushViewController:upvc animated:YES];
            }
                break;
            case 2:
            {//当前营业额
                BgetMoneyJLViewController *upvc = [[BgetMoneyJLViewController alloc]init];
                upvc.moneyStr =  weakSelf.headerView.dangqianLB.text;
                upvc.typeSYstr = @"2";
                upvc.titleStr = @"当前营业额";
                [self.navigationController pushViewController:upvc animated:YES];
            }
                break;
            case 3:
            {//历史营业额
                BgetMoneyJLViewController *upvc = [[BgetMoneyJLViewController alloc]init];
                upvc.moneyStr =  weakSelf.headerView.lishiLB.text;
                upvc.typeSYstr = @"3";
                upvc.titleStr = @"历史营业额";
                [self.navigationController pushViewController:upvc animated:YES];
            }
                break;
                
            default:
                break;
        }
        
   
    };
  
    
    
    [self showheader];
    [self request];
}

-(void)request{
    kWeakSelf;
    [NetWorkTool POST:BMonyeassetsManage param:nil success:^(id dic) {
        NSDictionary *mydic =[dic objectForKey:@"data"];
      
        weakSelf.headerView.jinriYongyee.text = [mydic objectForKey:@"todayAssets"];
        weakSelf.headerView.dangqianLB.text = [mydic objectForKey:@"setAssets"];
        weakSelf.headerView.lishiLB.text = [mydic objectForKey:@"allAssets"];

        self.mydateSource = [BadminMoneyModel mj_objectArrayWithKeyValuesArray:[mydic objectForKey:@"list"]];
//        [DYModelMaker DY_makeModelWithDictionary:[[mydic objectForKey:@"list"] firstObject] modelKeyword:@"" modelName:@""];
        [weakSelf.myTableView reloadData];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
    
    
}
-(void)showheader{
    UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 184)];
    UILabel *contertLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 178, KWIDTH, 16)];
    contertLb.textAlignment = NSTextAlignmentCenter;
    contertLb.textColor = [UIColor colorWithHexString:@"#979797"];
    contertLb.font = FontSize(11);
    contertLb.text = [NSString stringWithFormat:@"一  仅显示一年内的结算历史  一"];
    [head addSubview:contertLb];
    self.myTableView.tableFooterView = head;
    
    
    
    self.myTableView.tableHeaderView= _headerView;

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
        return 86;
        
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _mydateSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BadminMoneyListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BadminMoneyListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    BadminMoneyModel *poM0del = [_mydateSource safeObjectAtIndex:indexPath.row];
    if ( [poM0del.type integerValue] == 1) {
        cell.titleLb.text = [NSString stringWithFormat:@"结算-微信"];

    }else   if ( [poM0del.type integerValue] == 2){
        cell.titleLb.text = [NSString stringWithFormat:@"结算-支付宝"];

    }
    cell.shenqingTimeLb.text = poM0del.create_time;
    cell.yujiTimeLB.text = poM0del.update_time;
    
    float floatStr = [poM0del.app_count_money floatValue]; //字符串转float
    //NSLog(@"- - - - - - - - %f", floatStr);
    NSString *resultStr = [NSString stringWithFormat:@"%.2f",floatStr];
    //NSLog(@"%@ 有没有保留两位小数",resultStr);
    cell.moneyLB.text = [NSString stringWithFormat:@"-%@",resultStr];

    
    if ([poM0del.state integerValue] == 0) {
        
        cell.stateLB.text = @"审核中";
        cell.stateLB.textColor = [UIColor colorWithHexString:@"#999999"];
    }else   if ([poM0del.state integerValue] == 1) {
        
        cell.stateLB.text = @"已到账";
        cell.stateLB.textColor = [UIColor colorWithHexString:@"#CA9F61"];

    }
    
    

    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BadminMoneyModel *model = [_mydateSource safeObjectAtIndex:indexPath.row];
    BillDetailViewController *VC = [BillDetailViewController new];
    VC.from = @"1";
    VC.model = model;
    [self.navigationController pushViewController:VC animated:YES];
}


-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStyleGrouped)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
                _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
                    [_myTableView.mj_header beginRefreshing];
                }];
        
        [_myTableView registerNib:[UINib nibWithNibName:@"BadminMoneyListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BadminMoneyListTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end

